const dataModule = {
    // 模拟用户数据
    users: [
        { username: 'admin', password: 'admin123' },
        { username: 'test', password: 'test123' }
    ],

    books: [
        {
            id: 1,
            title: '三体',
            author: '刘慈欣',
            publishYear: 2008,
            status: '可借阅',
            category: '科幻',
            coverUrl: './img/三体.jpg'
        },
        {
            id: 2,
            title: '活着',
            author: '余华',
            publishYear: 1993,
            status: '可借阅',
            category: '文学',
            coverUrl: './img/活着.jpg'
        },
        {
            id: 3,
            title: '百年孤独',
            author: '加西亚·马尔克斯',
            publishYear: 1967,
            status: '已借出',
            category: '文学',
            coverUrl: './img/百年孤独.jpg'
        },
        {
            id: 4,
            title: 'JavaScript高级程序设计',
            author: '沈超',
            publishYear: 2025,
            status: '可借阅',
            category: '计算机',
            coverUrl: './img/JavaScript高级程序设计.jpg'
        }
    ],

    borrowings: [
        {
            id: 1,
            bookId: 1,
            userId: 'admin',
            borrowDate: '2024-03-15',
            returnDate: null
        },
        {
            id: 2,
            bookId: 2,
            userId: 'admin',
            borrowDate: '2024-03-10',
            returnDate: '2024-03-14'
        },
        {
            id: 3,
            bookId: 4,
            userId: 'admin',
            borrowDate: '2024-03-01',
            returnDate: '2024-03-08'
        }
    ],

    // 获取所有图书
    getAllBooks() {
        return this.books;
    },

    getBookById(id) {
        return this.books.find(book => book.id === id);
    },

    validateUser(username, password) {
        return this.users.find(user => 
            user.username === username && user.password === password
        );
    },

    addUser(username, password) {
        // 检查用户名是否已存在
        if (this.users.some(user => user.username === username)) {
            return false;
        }
        this.users.push({ username, password });
        return true;
    },

    getBorrowings() {
        const currentUser = sessionStorage.getItem('currentUser');
        return this.borrowings.filter(b => b.userId === currentUser);
    },

    getBorrowingById(id) {
        return this.borrowings.find(b => b.id === id);
    },

   
    addBorrowing(bookId) {
        const currentUser = sessionStorage.getItem('currentUser');
        const newBorrowing = {
            id: this.borrowings.length + 1,
            bookId: bookId,
            userId: currentUser,
            borrowDate: new Date().toLocaleDateString(),
            returnDate: null
        };
        this.borrowings.push(newBorrowing);
        return newBorrowing;
    }
}; 